<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="化肥种类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="typeOfFertilizer">
              <a-input v-model="model.typeOfFertilizer" placeholder="请输入化肥种类"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="施肥时间"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="timeOfFertilization"
            >
              <j-date placeholder="请选择施肥时间" v-model="model.timeOfFertilization" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="施肥作物" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fertilizedCrop">
              <a-input v-model="model.fertilizedCrop" placeholder="请输入施肥作物"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="施肥量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="amountFertilizer">
              <a-input-number v-model="model.amountFertilizer" placeholder="请输入施肥量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="施肥人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fertilizeMan">
              <a-input v-model="model.fertilizeMan" placeholder="请输入施肥人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="所属地块" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fieldId">
              <j-dict-select-tag
                type="list"
                v-model="model.fieldId"
                dictCode="zzzs_dand_plot_data,name_of_land,id"
                placeholder="请选择所属地块"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否展示" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <a-switch
                checked-children="显示"
                un-checked-children="不显示"
                default-checked
                v-model="model.statusBoolean"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'ZzzsFertilizationManagementForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        typeOfFertilizer: [{ required: true, message: '请输入化肥种类!' }],
        timeOfFertilization: [{ required: true, message: '请输入施肥时间!' }],
        fertilizedCrop: [{ required: true, message: '请输入施肥作物!' }],
        amountFertilizer: [{ required: true, message: '请输入施肥量!' }],
        fertilizeMan: [{ required: true, message: '请输入施肥人!' }],
        fieldId: [{ required: true, message: '请输入所属地块!' }],
      },
      url: {
        add: '/modules/zzzsFertilizationManagement/add',
        edit: '/modules/zzzsFertilizationManagement/edit',
        queryById: '/modules/zzzsFertilizationManagement/queryById',
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (that.model.statusBoolean) {
          that.model.status = 1
        } else {
          that.model.status = 0
        }
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>